package com.tanhua.dubbo.service;


import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class ProducerService {
    @Value("${spring.mq.quanzi.topic.address}")
    private String address;

    public void send(String topic ,String msg){
        try {
        //1.创建一个发送消息的 生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.设置命名服务器地址和端口
            producer.setNamesrvAddr(address);
        //3.启动生产者对象
        producer.start();
        //4.创建消息对象
        Message cont = new Message(topic,msg.getBytes("utf-8"));
            //5.发送消息(同步消息)
            SendResult result = producer.send(cont);
            System.out.println("发送成功后的返回结果: "+result);
            //6.关闭发送者客户端
            producer.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
